package com.ruoyi.match.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.match.domain.Teams;
import com.ruoyi.match.service.UserInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.match.domain.MemberTeam;
import com.ruoyi.match.service.IMemberTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 队员队伍Controller
 * 
 * @author zhangrui
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/match/MemberTeam")
public class MemberTeamController extends BaseController
{
    @Autowired
    private IMemberTeamService memberTeamService;


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询队员队伍列表
     */
    @PreAuthorize("@ss.hasPermi('match:MemberTeam:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberTeam memberTeam)
    {
        startPage();
        List<MemberTeam> list = memberTeamService.selectMemberTeamList(memberTeam);
        return getDataTable(list);
    }

    /**
     * 导出队员队伍列表
     */
    @PreAuthorize("@ss.hasPermi('match:MemberTeam:export')")
    @Log(title = "队员队伍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberTeam memberTeam)
    {
        List<MemberTeam> list = memberTeamService.selectMemberTeamList(memberTeam);
        ExcelUtil<MemberTeam> util = new ExcelUtil<MemberTeam>(MemberTeam.class);
        util.exportExcel(response, list, "队员队伍数据");
    }

    /**
     * 获取队员队伍详细信息
     */
    @PreAuthorize("@ss.hasPermi('match:MemberTeam:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(memberTeamService.selectMemberTeamByUserId(userId));
    }

    /**
     * 新增队员队伍
     */
/*    @PreAuthorize("@ss.hasPermi('match:MemberTeam:add')")*/
    @Log(title = "队员队伍", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MemberTeam memberTeam)
    {
        // 获取当前用户 ID
        Long userId = SecurityUtils.getUserId();
        // 将当前用户 ID 赋值到 memberTeam 对象中
        memberTeam.setUserId(userId);
        return toAjax(memberTeamService.insertMemberTeam(memberTeam));
    }

    /**
     * 修改队员队伍
     */
//    @PreAuthorize("@ss.hasPermi('match:MemberTeam:edit')")
    @Log(title = "队员队伍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberTeam memberTeam)
    {
        return toAjax(memberTeamService.updateMemberTeam(memberTeam));
    }

    /**
     * 删除队员队伍
     */
//    @PreAuthorize("@ss.hasPermi('match:MemberTeam:remove')")
    @Log(title = "队员队伍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(memberTeamService.deleteMemberTeamByUserIds(userIds));
    }

    /**
     * 队长踢出队员
     */
//    @PreAuthorize("@ss.hasPermi('match:MemberTeam:remove')")
    @Log(title = "队员队伍", businessType = BusinessType.DELETE)
    @PostMapping("/removeMember")
    public AjaxResult removeMember(String userName, Long teamId)
    {
        MemberTeam memberTeam = new MemberTeam();
        memberTeam.setUserId(userInfoService.selectUserId(userName));
        memberTeam.setTeamId(teamId);
        return toAjax(memberTeamService.removeMember(memberTeam));
    }



    //用户自己退出队伍
    @DeleteMapping("/leaveTeam/{teamId}")
    public AjaxResult leaveTeam(@PathVariable("teamId") Long teamId)
    {
        // 获取当前用户 ID
        Long userId = SecurityUtils.getUserId();
        System.out.println("userId:"+userId);
        return toAjax(memberTeamService.leaveTeam(userId, teamId));
    }
}
